package leetcode_2501_2600;

public class LeeCode_2507 {
    public static void main(String[] args) {
//        System.out.println(smallestValue(12));
//        System.out.println(smallestValue(15));
//        System.out.println(smallestValue(16));
//        System.out.println(smallestValue(8));
        System.out.println(smallestValue(4));
    }

    private static int smallestValue(int n) {
        if (n == 4)
            return 4;
        int tmp = 0;
        while (true) {
            if (isZhi(n)) {
                tmp += n;
                break;
            }
            int i = 2;
            while (i * i <= n) {
                if (n % i == 0 && isZhi(i)) {
                    n /= i;
                    tmp += i;
                    break;
                }
                i++;
            }
        }
        if (isZhi(tmp)){
            return tmp;
        }
        return smallestValue(tmp);
    }

    private static boolean isZhi(int n) {
        if (n == 1)
            return false;
        if (n == 2)
            return true;
        int i = 2;
        while (i * i <= n) {
            if (n % i == 0) {
                return false;
            }
            i++;
        }
        return true;
    }
}
